有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在JTextPane中为文本和下划线设置不同的颜色?

刚刚尝试在JTextPane中给文本上色,但问题是文本和下划线不能有不同的颜色。我该怎么做,或者说这可能吗?下面的示例以红色打印所有文本和下划线

JTextPane pane = new JTextPane();

StyleContext context = new StyleContext();

Style style = pane.addStyle("Black", null);
StyleConstants.setAlignment(style, StyleConstants.ALIGN_RIGHT);
StyleConstants.setFontSize(style, 14);
StyleConstants.setSpaceAbove(style, 4);
StyleConstants.setSpaceBelow(style, 4);
StyleConstants.setForeground(style, Color.BLACK);

StyledDocument document = pane.getStyledDocument();


style = pane.addStyle("Red Underline", style);
StyleConstants.setForeground(style, Color.RED);
StyleConstants.setUnderline(style, true);

pane.getDocument().insertString(0,  "Test String", style);

共 (3) 个答案

  1. # 1 楼答案

    基本上,您需要创建3个类:

    • 您需要扩展javax.swing.text.LabelView以执行修改视图的操作(无论是否添加彩色下划线)。您将重写paint(Graphics, Shape)方法。您可以使用重写类中的此行访问属性-属性应该是执行文本附加操作(如添加下划线)的触发器

      getElement().getAttributes().getAttribute("attribute name");

    • 您需要创建一个新的ViewFactory并覆盖create方法。在执行此操作时,处理所有元素类型是很重要的(否则,事情将无法正确显示)。

    • 您需要创建一个StyledEditorKit来告诉窗格使用哪个ViewFactory

    下面是一个简化且可运行的示例:

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.plaf.basic.BasicTextPaneUI;
    import javax.swing.text.*;
    
    public class TempProject extends JPanel{
    
    
        public static void main(String args[])    {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    
                    //Adding pane
                    JTextPane pane = new JTextPane();
                    pane.setEditorKit(new CustomEditorKit());
                    pane.setText("Underline With Different Color");
    
                    //Set Style
                    StyledDocument doc = (StyledDocument)pane.getDocument();
                    MutableAttributeSet attrs = new SimpleAttributeSet();
                    attrs.addAttribute("Underline-Color", Color.red);
                    doc.setCharacterAttributes(0, doc.getLength()-1, attrs, true);
    
                    JScrollPane sp = new JScrollPane(pane);
                    frame.setContentPane(sp);  
                    frame.setPreferredSize(new Dimension(400, 300));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
    
    
                }
            });
        }
    
        public static class CustomEditorKit extends StyledEditorKit{
    
            public ViewFactory getViewFactory(){
                return new CustomUI();
            }
        }
    
        public static class CustomUI extends BasicTextPaneUI{
            @Override
            public View create(Element elem){
                View result = null;
                String kind = elem.getName();
                if(kind != null){
                    if(kind.equals(AbstractDocument.ContentElementName)){
                        result = new MyLabelView(elem);
                    } else if(kind.equals(AbstractDocument.ParagraphElementName)){
                        result = new ParagraphView(elem);
                    }else if(kind.equals(AbstractDocument.SectionElementName)){
                        result = new BoxView(elem, View.Y_AXIS);
                    }else if(kind.equals(StyleConstants.ComponentElementName)){
                        result = new ComponentView(elem);
                    }else if(kind.equals(StyleConstants.IconElementName)){
                        result = new IconView(elem);
                    } else{
                        result = new LabelView(elem);
                    }
                }else{
                    result = super.create(elem);
                }
    
                return result;
            }
        }
    
        public static class MyLabelView extends LabelView{
    
            public MyLabelView(Element arg0) {
                super(arg0);
            }
    
            public void paint(Graphics g, Shape a){
                super.paint(g, a);
                //Do whatever other painting here;
                Color c = (Color)getElement().getAttributes().getAttribute("Underline-Color");
                if(c != null){
                    int y = a.getBounds().y + (int)getGlyphPainter().getAscent(this);
                    int x1 = a.getBounds().x;
                    int x2 = a.getBounds().width + x1;
    
                    g.setColor(c);
                    g.drawLine(x1, y, x2, y);
                }
    
            }
    
        }
    
    }
    

    以下是指向另一个示例代码的链接:

    http://java-sl.com/tip_colored_strikethrough.html

    这个答案主要是为了子孙后代,我认为添加一个简化版本的链接代码和解释将有助于使事情更容易理解

  2. # 2 楼答案

    • for examplefor Html

    • Document返回要查看的模型,可以确定行开始/结束的索引